package com.gf.ssjk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.common.BaseController;
import com.gf.common.EncodingUtil;
import com.gf.common.ResultMessage;
import com.gf.common.UUIDGenerator;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.service.MyBatisService;
import com.gf.ssjk.model.Dtxx;
import com.gf.ssjk.service.DtxxService;

/**
 * 实时监控管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/ssjk")
public class SsjkController extends BaseController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SsjkController.class);
	
	@Autowired
	private MyBatisService myBatisService;
	
	@Autowired
	private DtxxService dtxxService;
	
	/**
	 * 实时监控电梯列表页面
	 * @return
	 */
	@RequestMapping(value="/lDtxx", method=RequestMethod.GET)
	public String dtglList(){
		LOGGER.info("进入电梯管理页面");
		return "ssjk/dtxxList";
	}
	
	/**
	 * 实时监控电梯列表
	 * @return
	 */
	@RequestMapping(value="/dataGrid",method=RequestMethod.POST)
	@ResponseBody
	public Flexigrid dtListData(Flexigrid flex,Dtxx dtxx){
		LOGGER.info("电梯信息查询");
		dtxx.setDtxxSydwName(EncodingUtil.getInstance().decode(dtxx.getDtxxSydwName()));
		dtxx.setDtxxWbdwName(EncodingUtil.getInstance().decode(dtxx.getDtxxWbdwName()));
		dtxx.setDtxxWbzrr(EncodingUtil.getInstance().decode(dtxx.getDtxxWbzrr()));
		flex.setObj(dtxx);
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList("dtxx.dtxxList", flex);
		int total = myBatisService.count("dtxx.total", flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
	
	/**
	 * 实时监控电梯添加页面
	 * @return
	 */
	@RequestMapping(value ="/fDtxx",method=RequestMethod.GET)
	public String dtAddForward(){
		LOGGER.info("进入电梯添加页面");
		return "ssjk/dtxxAdd";
	}
	
	/**
	 * 电梯信息添加
	 * @return
	 */
	@RequestMapping(value = "/addDtxx", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage dtxxAdd(Dtxx dtxx){
		LOGGER.info("准备新建电梯信息");
		dtxx.setId(UUIDGenerator.getInstance().getUUID32());
		Integer result=dtxxService.addDtxx(dtxx);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("电梯信息保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("电梯信息保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 修改电梯管理页面
	 * @param dtxxId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public String toUpdate(String dtxxId,Model model){
		LOGGER.info("准备进入电梯修改页面");
		Dtxx dtxx=dtxxService.getDtxxById(dtxxId);
		if(dtxx!=null){
			model.addAttribute(dtxx);
		}
		return "ssjk/dtxxUpdate";
	}
	
	/**
	 * 根据编号获取电梯信息
	 * @return
	 */
	@RequestMapping(value="/getDtxxByBh",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getDtxxByBh(String dtxxBh){
		LOGGER.info("获取电梯信息");
		Map<String, Object> map=new HashMap<String, Object>();
		List<Dtxx> dtxxList= dtxxService.getDtxxByBh(dtxxBh);
		map.put("dtxxList", dtxxList);
		return map;
	}
	
	/**
	 * 修改电梯信息
	 * @param dtxx
	 * @return
	 */
	@RequestMapping(value = "/updateDtxx", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage dtxxUpdate(Dtxx dtxx){
		LOGGER.info("准备修改电梯信息");
		Integer result=dtxxService.updateDtxx(dtxx);
		if(result>0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("电梯修改成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("电梯修改失败！");
		}
		return super.resultMessage;
	}

	/**
	 * 删除电梯信息
	 * @return
	 */
	@RequestMapping(value = "/deleteDtxx", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage dtxxDelete(String dtxxId){
		LOGGER.info("删除电梯信息");
		Integer result=dtxxService.deleteDtxx(dtxxId);
		if(result>0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("电梯信息删除成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("电梯信息删除失败！");
		}
		return super.resultMessage;
	}
}
